package com.mediocre.api.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/*
    这里必须用static修饰TestService类，否则报如下错误：
    java: 内部类com.mediocre.api.SentinelRateLimitingApplication.TestService中的静态声明非法
    修饰符 'static' 仅允许在常量变量声明中使用
     */
@Slf4j
@Service
public class TestService {

    @SentinelResource(value = "doSomething")
    public void doSomething(String str) {
        log.info(str);
    }

    // 2 实现限流的异常处理
    @SentinelResource(value = "doSomeThing2", blockHandler = "exceptionHandler")
    public void doSomeThing2(String str) {
        log.info(str);
    }

    // 2 实现限流的异常处理: 限流与阻塞处理
    public void exceptionHandler(String str, BlockException ex) {
        log.error("blockHandler: " + str, ex);
    }


    // 3 实现熔断降级
    @SentinelResource(value = "doSomeThing3", fallback = "fallbackHandler")
    public void doSomeThing3(String str) {
        log.info(str);
        throw new RuntimeException("发生异常");
    }

    // 3 实现熔断降级的异常处理
    public void fallbackHandler(String str) {
        log.error("fallbackHandler：" + str);
    }
}
